#include "main.h"
#include "KeyBoard.h"
#include "string.h"
#include "usart.h"
#include "BTK05.h"
#include "RGB.h"
#include "stdio.h"
#include "tim.h"

#define Control_Key 0x00
#define xxCK Control_Key

#define Special_Key 0x00
#define xxSK Special_Key

uint8_t COPY_PASTE_flag = 0;
uint8_t Hong_Flag = 0;
uint8_t flag_2 = 0;
uint16_t tim3_Count_Time = 0;
uint8_t tim3_Count_Time_Flag = 0;
uint16_t RGB_Speed = 75;
uint8_t Wave_Type = 0;
uint8_t Point_Water_Flag = 0;
uint8_t BUZZ_COUNT_FLAG = 0;
uint8_t Count_Time_BUZZ = 0;
uint8_t BUZZ_FLAG = 0;
uint8_t Light_Grade = 8;
uint8_t Light = 8;

const char * myKeyBoard_KeyMap[6][20] = 
{
	"Esc","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","PrtSc","ScrollLock","PauseBreak","","","","",
	"~","!","@","#","$","%","^","&","*","(",")","-","+","BackSpace","Insert","Home","NumLock","/","*","-",
	"Tab","Q","W","E","R","T","Y","U","I","O","P","{","}","|","Delete","End","7","8","9","+",
	"CapsLock","A","S","D","F","G","H","J","K","L",";","'","Enter","","","PageUp","4","5","6","",
	"LShift","Z","X","C","V","B","N","M",",",".","/","RShift","Up","","","PageDown","1","2","3","Enter",
	"LCtrl","Win","LAlt","Space","RAlt","FN","Menu","RCtrl","Left","Down","Right","V+","V-","V0","COPY","PASTE","0","",".",""
};

const unsigned char myKeyBoard_KeyMap_ATValue[6][20] = 
{
	0x29,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x00,0x00,0x00,0x00,
	0x35,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x2D,0x2E,0x2A,0x49,0x4A,0x53,0x54,0x55,0x56,
	0x2B,0x14,0x1A,0x08,0x15,0x17,0x1C,0x18,0x0C,0x12,0x13,0x2F,0x30,0x31,0x4C,0x4D,0x5F,0x60,0x61,0x57,
	0x39,0x04,0x16,0x07,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x33,0x34,0x28,0x00,0x00,0x4B,0x5C,0x5D,0x5E,0x00,
	xxCK,0x1D,0x1B,0x06,0x19,0x05,0x11,0x10,0x36,0x37,0x38,xxCK,0x52,0x00,0x00,0x4E,0x59,0x5A,0x5B,0x58,
	xxCK,xxCK,xxCK,0x2C,xxCK,xxSK,xxSK,xxCK,0x50,0x51,0x4F,0x80,0x81,0x7F,0x7C,0x7D,0x62,0x00,0x63,0x00
};


const uint16_t key_Col_Pin[20] = 
{
	COL0_Pin,
	COL1_Pin,
	COL2_Pin,
	COL3_Pin,
	COL4_Pin,
	COL5_Pin,
	COL6_Pin,
	COL7_Pin,
	COL8_Pin,
	COL9_Pin,
	COL10_Pin,
	COL11_Pin,
	COL12_Pin,
	COL13_Pin,
	COL14_Pin,
	COL15_Pin,
};

typedef struct
{
	unsigned char ATKeyData;
	unsigned char ROW_N;
	unsigned char COL_N;
}AT_KEYDATA_Structure_Typedef;

AT_KEYDATA_Structure_Typedef ATKeyData[6];
unsigned char ATKeyControlByte5 = 0x00;

KeyState_enumTypedef myKeyBoard_KeyState[6][16] = {KEYUNPRESSED};
KeyState_enumTypedef myKeyBoard_KeyState_Pre[6][16] = {KEYUNPRESSED};

uint8_t LOGO_LED_Status = 0;

unsigned char myKeyBoard_KeyStateChangedFlag = 0;
unsigned char myKeyBoard_KeyIsControl = 1;
unsigned char g_myKeyBoard_DataWaitForUploadFlag = 0;

void myKeyBoard_ControlKeyProcess(uint8_t rowi, uint8_t colj);
void myKeyBoard_KeyScan_rowScan(GPIO_TypeDef* col_GPIOPort, uint8_t  col_GPIOPin_Index,const uint16_t *	p_col_GPIOPin);

void myKeyBoard_ScanKeyAndUpdataATBuffer()
{
	myKeyBoard_KeyScan();//扫描键盘矩阵
	myKeyBoard_JudgeKeyStateChange();//判断这一次和上一次有没有变化
	if(myKeyBoard_KeyStateChangedFlag)//如果有变化
	{
		myKeyBoard_KeyStateChangedFlag = 0;
		myKeyBoard_UpdataATDataPack();//将键盘文件更新并传入BTK05数组中
		g_myKeyBoard_DataWaitForUploadFlag = 1;//数据上传完成
	}
}

enum
{
	none = 0,
	Mute = 1,
	VolumeUp = 2,
	VolumeDown = 3,
	PlayORPause = 4,
	Stop = 5
}specialKeyenum = none;

void ConsumerKeyProcess(void)//处理FN组合功能键，并发送给BTK05
{
	BTK05_ConsumerPack[5] = 0x00;//数组清零
	switch(specialKeyenum)
	{
		case Mute:
			BTK05_ConsumerPack[5] |= (0x01 << 0); 
			break;
		case VolumeUp:
			BTK05_ConsumerPack[5] |= (0x01 << 2);
			break;
		case VolumeDown:
			BTK05_ConsumerPack[5] |= (0x01 << 1);
			break;
		case PlayORPause:
			BTK05_ConsumerPack[5] |= (0x01 << 3);
			break;
		case Stop:
			BTK05_ConsumerPack[5] |= (0x01 << 4);
			break;
		default:
			break;
	}
	
	BTK05_UART_SendKeyData(BTK05_ConsumerPack,8);
	BTK05_ConsumerPack[5] = 0x00;
	BTK05_UART_SendKeyData(BTK05_ConsumerPack,8);
}


void myKeyBoard_UpdataATDataPack()
{
	uint8_t i;
	uint8_t j;
	uint8_t k;
	uint8_t AlreadyExistFlag = 0;
	ATKeyControlByte5 = 0x00;
	
	if(myKeyBoard_KeyState[5][5] == KEYPRESSED)//如果fn键被按下
	{
		
		if(myKeyBoard_JudgeKeyPressWithName("F7"))//fn+f7音量减
		{
			specialKeyenum = VolumeDown;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F8"))//fn+f8音量加
		{
			specialKeyenum = VolumeUp;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F9"))//fn+f9音量关
		{
			specialKeyenum = Mute;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F10"))
		{
			specialKeyenum = PlayORPause;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F11"))
		{
			specialKeyenum = Stop;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Up"))
		{
			if(Light_Grade < 8)
			{
				RGB_STATE.R = RGB_STATE.R * (Light_Grade + 1) / Light_Grade ;
				RGB_STATE.G = RGB_STATE.G * (Light_Grade + 1) / Light_Grade;
				RGB_STATE.B = RGB_STATE.B * (Light_Grade + 1) / Light_Grade;
				Light = Light * (Light_Grade + 1) / Light_Grade;
				Light_Grade++;
			}
			Full_COLOR(RGB_STATE);
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Down"))
		{
			if(Light_Grade == 8)
			{
				Light_Grade--;
				RGB_STATE.R = RGB_STATE.R * Light_Grade / 8;
				RGB_STATE.G = RGB_STATE.G * Light_Grade / 8;
				RGB_STATE.B = RGB_STATE.B * Light_Grade / 8;
				Light = Light * Light_Grade / 8;
			}
			else if(Light_Grade > 2 && Light_Grade != 8)
			{
				
				RGB_STATE.R = RGB_STATE.R / Light_Grade * (Light_Grade - 1);
				RGB_STATE.G = RGB_STATE.G / Light_Grade * (Light_Grade - 1);
				RGB_STATE.B = RGB_STATE.B / Light_Grade * (Light_Grade - 1);
				Light = Light / Light_Grade * (Light_Grade - 1);
				Light_Grade--;
			}
			Full_COLOR(RGB_STATE);
		}
		
		if(specialKeyenum != none)//把FN组合键发送给给BTK05
		{
			if(g_USBModeFlag != 1)
			{
				ConsumerKeyProcess();
			}
			specialKeyenum = none;
		}
	}
	
	if(myKeyBoard_KeyState[5][6] == KEYPRESSED)//如果menu键被按下
	{
		if(myKeyBoard_JudgeKeyPressWithName("PauseBreak"))//menu+PauseBreak切换模式
		{
			if(!g_USBModeFlag)//如果之前是蓝牙模式，切换成USB模式
			{
				USB_Mode();
				
			}
			else//如果之前是USB模式，切换成蓝牙模式
			{
				BLUETEETH_Mode();
			}	
		}
		else if(myKeyBoard_JudgeKeyPressWithName("ScrollLock"))//menu+ScrollLock 亮灯
		{
			if(g_USBModeFlag)	//如果是USB模式
			{
				if(!LOGO_LED_Status)//如果灯没亮，灯亮
				{
					LOGO_LED_Status = 1;
				}
				else//灯亮
				{
					LOGO_LED_Status = 0;
				}
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Delete"))//menu+Delet 删除蓝牙信息
		{
			BTK05_DeleteConnectInfo();
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Insert"))//menu+Insert 蓝牙配对
		{
			BTK05_PairMode();
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Home"))//menu+Home BTK05重置
		{
			BTK05_RESET();
		}
		else if(myKeyBoard_JudgeKeyPressWithName("End"))//menu+End 蓝牙进入休眠模式
		{
			BTK05_Sleep();
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F8"))//menu+F8 开启宏程序
		{
			if(COPY_PASTE_flag == 0 && tim3_Count_Time_Flag == 0)
			{
				COPY_PASTE_flag = ON;
				tim3_Count_Time_Flag = ON;
				HAL_TIM_Base_Start_IT(&htim3);	
			}
			
			if(COPY_PASTE_flag == 1 && tim3_Count_Time_Flag == 0)
			{
				COPY_PASTE_flag = OFF;
				tim3_Count_Time_Flag = OFF;
				HAL_TIM_Base_Start_IT(&htim3);
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F7"))//menu+F7 开启自定义快捷键模式
		{
			if(Hong_Flag == 0)
			{
				Hong_Flag = 1;
				Count_RGB_Flag = 0;
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Right"))//menu+→ 保存右滑快捷键
		{
			if(Hong_Flag == 1)
			{
				Hong_Flag = 2;
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Left"))//menu+← 保存左滑快捷键
		{
			if(Hong_Flag == 1)
			{
				Hong_Flag = 3;
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Up"))//menu+↑ 加速
		{
			if(RGB_Speed > 48)
			{
				HAL_Delay(30);
				RGB_Speed = RGB_Speed * 0.8;
				
			}
			else if(RGB_Speed <= 48)
			{
				Full_COLOR(RED);
				HAL_Delay(500);
				Full_COLOR(RGB_STATE);
			}	
		}
		else if(myKeyBoard_JudgeKeyPressWithName("Down"))//menu+↓ 减速
		{
			if(RGB_Speed < 116)
			{
				HAL_Delay(30);
				RGB_Speed = RGB_Speed / 0.8;
			}
			else if(RGB_Speed >= 116)
			{
				Full_COLOR(RED);
				HAL_Delay(500);
				Full_COLOR(RGB_STATE);
			}
		}
		//没必要写蓝牙唤醒，因为按一下键盘就醒了
		
		//·····················灯光部分························//
		
		else if(myKeyBoard_JudgeKeyPressWithName("F12"))
		{
			Full_COLOR(WHITE);
			RGB_STATE = WHITE;
			Color_Flag = LIGHT_COLOR_STATE;
			Light_Grade = 8;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F11"))
		{
			Full_COLOR(LIGHT_GREEN);
			RGB_STATE = LIGHT_GREEN;
			Color_Flag = LIGHT_COLOR_STATE;
			Light_Grade = 8;
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F10"))
		{
			Count_RGB_Flag = 5;
			Light_Grade = 8;			
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F9"))
		{
			if(BUZZ_FLAG == 1)
			{
				BUZZ_FLAG = 0;
				HAL_Delay(500);
			}
			else if(BUZZ_FLAG == 0)
			{
				BUZZ_FLAG = 1;
				HAL_Delay(500);
			}
		}
		
		else if(myKeyBoard_JudgeKeyPressWithName("F1"))//开启波浪模式
		{
			if(RGB_STATE.R != BLACK.R || RGB_STATE.G != BLACK.G || RGB_STATE.B != BLACK.B)
			{
				Count_RGB_Flag = 1;	
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F2"))
		{
			if(RGB_STATE.R != BLACK.R || RGB_STATE.G != BLACK.G || RGB_STATE.B != BLACK.B)
			{
				Count_RGB_Flag = 2;	
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F3"))
		{
			if(RGB_STATE.R != BLACK.R || RGB_STATE.G != BLACK.G || RGB_STATE.B != BLACK.B)
			{
				Count_RGB_Flag = 3;	
			}
		}
		else if(myKeyBoard_JudgeKeyPressWithName("F4"))
		{
			//打开点水特效
			
			//如果有其他特效
			if(Count_RGB_Flag != 0 && Count_RGB_Flag != 4 && Point_Water_Flag != 2)
			{
				Point_Water_Flag = 2;
			}
			else if(Point_Water_Flag == 2)
			{
				Point_Water_Flag = 0;
			}
			//如果没有其他特效
			else if(((RGB_STATE.R != BLACK.R) || (RGB_STATE.G != BLACK.G) || (RGB_STATE.B != BLACK.G)) && (Count_RGB_Flag != 4))
			{
				Count_RGB_Flag = 4;
			}
			
			else if(Count_RGB_Flag == 4)
			{
				Count_RGB_Flag = 0;
				Full_COLOR(RGB_STATE);
				HAL_Delay(500);
			}
		}
		
		else if(myKeyBoard_JudgeKeyPressWithName("Esc"))
		{
			Count_RGB_Flag = 0;
			Point_Water_Flag = 0;
			Full_COLOR(BLACK);
			RGB_STATE = BLACK;
			Color_Flag = BLACK_COLOR_STATE;
		}
	}
	
	//检测上次按下的按键是否还在继续按，如果没再继续按了就将ATKeyData的数据清除
	for(i = 0;i < 6;i++)
	{
		if(ATKeyData[i].ATKeyData != 0x00)
		{
			if(myKeyBoard_KeyState[ATKeyData[i].ROW_N][ATKeyData[i].COL_N] == KEYUNPRESSED)
			{
				ATKeyData[i].ATKeyData = 0x00;
			}
		}
	}
	
	
	for(i = 0;i < 6;i++)
	{
		for(j = 0;j < 16;j++)
		{
			if(myKeyBoard_KeyState[i][j] == KEYPRESSED)//如果按键被按下则处理键盘数据包
			{	
				myKeyBoard_ControlKeyProcess(i,j);//判断控制按键是否被按下
				
				if(myKeyBoard_KeyIsControl == 0)//如果控制按键没被按下
				{
					for(k = 0;k<6;k++)
					{
						if(ATKeyData[k].ATKeyData == myKeyBoard_KeyMap_ATValue[i][j])//判断这个按键是否已经被按下
						{
							AlreadyExistFlag = 1;
							break;
						}
					}
					if(AlreadyExistFlag == 0)//如果数据包中原本没有这个按键，则放入
					{
						//判断有没有空位，有空位就放进去
						for(k = 0;k<6;k++)
						{
							
							if(ATKeyData[k].ATKeyData == 0x00)
							{
								ATKeyData[k].ATKeyData = myKeyBoard_KeyMap_ATValue[i][j];
								ATKeyData[k].ROW_N = i;
								ATKeyData[k].COL_N = j;
								break;
							}
						}
					}
					else//如果这个按键已经存在，则不管它。
					{
						AlreadyExistFlag = 0;
					}
					myKeyBoard_KeyIsControl = 1;//控制按键标志位复位
				}
			}
		}
	}
	
	BTK05_ATKeyDataPack[4] = ATKeyControlByte5;//更新控制按键字节
	
	for(k = 0; k < 6; k++)
	{
		BTK05_ATKeyDataPack[6 + k] = ATKeyData[k].ATKeyData;//更新普通按键字节
	}
}

void myKeyBoard_ControlKeyProcess(uint8_t rowi,uint8_t colj)
{
	if(strcmp(myKeyBoard_KeyMap[rowi][colj],"LShift") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 1);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RShift") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 5);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"LCtrl") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 0);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RCtrl") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 4);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"LAlt") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 2);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"RAlt") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 6);
	}
	else if(strcmp(myKeyBoard_KeyMap[rowi][colj],"Win") == 0)
	{
		ATKeyControlByte5 = ATKeyControlByte5 | (0x01 << 3);
	}
	else
	{
		myKeyBoard_KeyIsControl = 0;
	}
}

void myKeyBoard_JudgeKeyStateChange()
{
	uint8_t i;
	uint8_t j;
	for(i = 0;i < 6;i++)//检查键盘状态是否发生改变
	{
		for(j = 0;j < 16;j++)
		{
			if(myKeyBoard_KeyState_Pre[i][j] != myKeyBoard_KeyState[i][j])//如果不一样，退出循环
			{
				myKeyBoard_KeyStateChangedFlag = 1;
				break;
			}
		}			
		if(myKeyBoard_KeyStateChangedFlag)
		{
			break;
		}	
	}	
	
	for(i = 0;i<6;i++)//将这次的状态传递到上次
	{
		for(j = 0;j<16;j++)
		{
			myKeyBoard_KeyState_Pre[i][j] = myKeyBoard_KeyState[i][j];
		}
	}
}


void myKeyBoard_KeyScan()//扫描键盘
{
	uint8_t i;
	for(i = 0;i < 6;i++)
	{
		myKeyBoard_KeyScan_rowScan(GPIOB,i,key_Col_Pin);
		HAL_Delay(2);
	}
	for(i = 6;i < 16;i++)
	{
		myKeyBoard_KeyScan_rowScan(GPIOD,i,key_Col_Pin);
		HAL_Delay(2);
	}
}

void BUZZ(uint8_t flag)
{
	if(flag == 1)
	{
		HAL_GPIO_WritePin(BUZZ_GPIO_Port,BUZZ_Pin,1);
		BUZZ_COUNT_FLAG = 1;
	}
}

void myKeyBoard_KeyScan_rowScan(GPIO_TypeDef * col_GPIOPort, uint8_t col_GPIOPin_Index,const uint16_t * p_col_GPIOPin)
{
	
	HAL_GPIO_WritePin(col_GPIOPort,*(p_col_GPIOPin + col_GPIOPin_Index),1);//写高电平
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 1)//第一行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 1)
		{
			myKeyBoard_KeyState[0][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[0][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1) == 1)//第二行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1) == 1)
		{
			myKeyBoard_KeyState[1][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[1][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2) == 1)//第三行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_2) == 1)
		{
			myKeyBoard_KeyState[2][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[2][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3) == 1)//第四行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3) == 1)
		{
			myKeyBoard_KeyState[3][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[3][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4) == 1)//第五行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4) == 1)
		{
			myKeyBoard_KeyState[4][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[4][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4) == 1)//第六行扫描
	{
		HAL_Delay(10);
		if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4) == 1)
		{
			myKeyBoard_KeyState[5][col_GPIOPin_Index] = KEYPRESSED;
			BUZZ(BUZZ_FLAG);
		}
	}
	else
	{
		myKeyBoard_KeyState[5][col_GPIOPin_Index] = KEYUNPRESSED;
	}
	
	HAL_GPIO_WritePin(col_GPIOPort,*(p_col_GPIOPin + col_GPIOPin_Index),0);
}


unsigned char myKeyBoard_JudgeKeyPressWithName(const char*keyName)//判断是不是这个按键背按下
{
	unsigned char i;
	unsigned char j;
	for(i = 0;i < 6;i++)
	{
		for(j = 0;j < 16;j++)
		{
			if(strcmp(myKeyBoard_KeyMap[i][j],keyName)==0)
			{
				if(myKeyBoard_KeyState[i][j] == KEYUNPRESSED)
				{
					return 0;
				}
				else
				{
					myKeyBoard_KeyState[i][j] = KEYUNPRESSED;
					return 1;
				}
			}
		}
	}
	return 0;
}


